package DAO;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Beans.DepositoBean;
import Beans.DepositoCentralBean;
import HibernateUtils.HibernateUtil;

public class HibernateDepositoDAO {
	private static HibernateDepositoDAO instancia = null;
	private static SessionFactory sf = null;

	public static HibernateDepositoDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateDepositoDAO();
		} 
		return instancia;
	}
	
	public void grabarDeposito(DepositoBean m){
		/*Abro sesion y grabo el objeto*/
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public List<DepositoBean> getDepositos() {
		Session session = sf.openSession();
		List<DepositoBean>depositos=session.createQuery("from DepositoBean m ").list();
		session.close();
		return depositos;
		
	}

	public void updateDeposito(DepositoBean m) {
		
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
		
	}

	public DepositoBean getDeposito(int nroDeposito) {
			
		Session session=sf.openSession();
		DepositoBean m;
		m=(DepositoBean)session.get(DepositoBean.class,nroDeposito);
		return m;
	

		
	}

	public DepositoBean buscarDeposito(DepositoBean movb) {
		Session session=sf.openSession();
		DepositoBean m;
		m=(DepositoBean)session.get(DepositoBean.class,movb.getIdDeposito());
		return m;
	}

	public DepositoCentralBean buscarDepositoCentral(DepositoCentralBean depb) {
		Session session=sf.openSession();
		DepositoCentralBean m=new DepositoCentralBean();
		m=(DepositoCentralBean)session.get(DepositoCentralBean.class,depb.getIdDeposito());
		return  m;
	}

	public void updateDepositoCentral(DepositoCentralBean dep) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(dep);
		session.flush();
		session.getTransaction().commit();
		session.close();
		
	}
}
